﻿package frEngine.primitives.lightingline.bullet
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import flash.geom.*;
    import frEngine.core.mesh.*;
    import frEngine.primitives.lightingline.*;

    public class LightingLine extends FrFlashLine
    {
        public var _maxFlyTime:Number = 0.5;
        public var _jumpFactor:Number = 4;
        public var startBoneName:String;
        public var startOffset:Vector3D;
        public var endBoneName:String;
        public var endOffset:Vector3D;
        private var _state:int = 1;
        private var _flyTime:Number = 0;
        private var _caster:Md5Mesh;
        private var _targets:Vector.<Md5Mesh>;
        private var _curTarget:int = 0;
        private var _reuse:Boolean = true;
        private var _playing:Boolean = false;
        public static const State_Fly:int = 1;
        public static const State_Hit:int = 2;
        public static const State_Finish:int = 3;

        public function LightingLine(param1:String, param2:Boolean, param3:RenderList)
        {
            this._targets = new Vector.<Md5Mesh>;
            super(param1, param2, param3);
            this.ChangeStateToFly();
            return;
        }// end function

        public function ReSetup(param1:Md5Mesh, param2:Vector.<Md5Mesh>) : void
        {
            this._caster = param1;
            this._targets = param2;
            this._curTarget = 0;
            this.ChangeStateToFly();
            return;
        }// end function

        override public function update(param1:int = 0) : void
        {
            super.update(param1);
            if (_escapeTime == 0)
            {
                return;
            }
            switch(this._state)
            {
                case State_Fly:
                {
                    break;
                }
                case State_Hit:
                {
                    break;
                }
                case State_Finish:
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (!this._lines || _lines.length != 0)
            {
            }
            return;
        }// end function

        private function ChangeStateToFly() : void
        {
            this._state = State_Fly;
            this._flyTime = 0;
            return;
        }// end function

        private function DoStateFly(param1:Number) : void
        {
            var _loc_2:Vector.<NoiseLine> = null;
            var _loc_3:int = 0;
            var _loc_4:Vector3D = null;
            var _loc_5:Vector3D = null;
            this._flyTime = this._flyTime + param1;
            if (this._flyTime >= this._maxFlyTime)
            {
                this.ChangeStateToHit();
            }
            else
            {
                _loc_2 = new Vector.<NoiseLine>;
                if (this._caster && this._targets && this._targets.length > 0)
                {
                    _loc_3 = 0;
                    while (_loc_3 <= this._curTarget)
                    {
                        
                        _loc_4 = _loc_3 == 0 ? (this.getPos(this._caster, this.startBoneName, this.startOffset)) : (this.getPos(this._targets[(_loc_3 - 1)], this.endBoneName, this.endOffset));
                        _loc_5 = this.getPos(this._targets[_loc_3], this.endBoneName, this.endOffset);
                        if (_loc_3 == this._curTarget)
                        {
                            _loc_5 = _loc_5.subtract(_loc_4);
                            _loc_5.scaleBy(Math.min(1, this._jumpFactor * this._flyTime / this._maxFlyTime));
                            _loc_5 = _loc_4.add(_loc_5);
                        }
                        _loc_2.push(new NoiseLine(_loc_4, _loc_5, 0));
                        _loc_3++;
                    }
                }
                this.SetLines(_loc_2);
            }
            return;
        }// end function

        private function ChangeStateToHit() : void
        {
            this._state = State_Hit;
            var _loc_1:String = this;
            var _loc_2:* = this._curTarget + 1;
            _loc_1._curTarget = _loc_2;
            return;
        }// end function

        private function DoStateHit(param1:Number) : void
        {
            if (this._curTarget < this._targets.length && this._targets[this._curTarget] != null)
            {
                this.ChangeStateToFly();
            }
            else
            {
                this.ChangeStateToFinish();
            }
            return;
        }// end function

        private function ChangeStateToFinish() : void
        {
            this._state = State_Finish;
            return;
        }// end function

        private function DoStateFinish(param1:Number) : void
        {
            if (!this._reuse)
            {
                this.disposeLines();
                this.dispose(false);
            }
            else
            {
                this._playing = false;
            }
            return;
        }// end function

        private function getPos(param1:Md5Mesh, param2:String = "Bip001 Pelvis", param3:Vector3D = null) : Vector3D
        {
            var _loc_4:Vector3D = null;
            var _loc_5:Vector3D = null;
            var _loc_6:Vector3D = null;
            if (param2)
            {
                _loc_5 = param1.targetMd5Controler.getGJointPoseByName(param2);
                if (_loc_5)
                {
                    if (param3)
                    {
                        _loc_4 = param1.world.transformVector(_loc_5.add(param3));
                    }
                    else
                    {
                        _loc_4 = param1.world.transformVector(_loc_5);
                    }
                }
                else
                {
                    param2 = null;
                }
            }
            if (!param2)
            {
                if (param3)
                {
                    _loc_6 = param1.world.transformVector(param3);
                }
                else
                {
                    _loc_6 = new Vector3D();
                    param1.world.copyColumnTo(3, _loc_6);
                }
                _loc_4 = _loc_6;
            }
            return _loc_4;
        }// end function

        public function get playing() : Boolean
        {
            return this._playing;
        }// end function

        public function set playing(param1:Boolean) : void
        {
            this._playing = param1;
            if (this._playing)
            {
                this._curTarget = 0;
                this.ChangeStateToFly();
            }
            return;
        }// end function

    }
}
